package com.tanhua.server.controller;

import com.tanhua.server.service.IMService;
import com.tanhua.server.utils.NoAuthorization;
import com.tanhua.server.vo.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/2/23 21:25
 * @version: 0.0.1
 * @modified By:
 */
@RestController
@RequestMapping("messages")
public class IMController {
    @Autowired
    private IMService imService;

    private static final Logger LOGGER= LoggerFactory.getLogger(IMController.class);
    /**
     * @description 添加好友
     * @author
     * @date
     * @param
     *@return
     */
    @PostMapping("contacts")
    public ResponseEntity<Void>contactUser(@RequestBody Map<String,Object>param){
        try {
            Long userId = Long.valueOf(param.get("userId").toString());
            Boolean bool=this.imService.contactUser(userId);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            LOGGER.error("添加联系人失败~ param"+param,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 查询联系人列表
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("contacts")
    public ResponseEntity<PageResult>queryContactsList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                  @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
                                                  @RequestParam(value = "keyword",required = false)String keyword){
        try {
            PageResult pageResult= this.imService.queryContactsList(page,pageSize,keyword);
            if(null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            LOGGER.error("查询联系人列表失败~ ",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }
    /**
     * @description 查询点赞列表
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("likes")
    public ResponseEntity<PageResult>queryMessageLikeList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                          @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult= this.imService.queryMessageLikeList(page,pageSize);
            if(null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            LOGGER.error("查询联系人点赞列表失败~ ",e);
        }
       return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 查询评论列表
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("comments")
    public ResponseEntity<PageResult>queryMessageCommentList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                             @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult=this.imService.queryMessageCommentList(page,pageSize);
            if(null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            LOGGER.error("查询联系人评论列表失败~ ",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 查询喜欢列表
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("loves")
    public ResponseEntity<PageResult>queryMessageLoveList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                          @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult=this.imService.queryMessageLoveList(page,pageSize);
            if(null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            LOGGER.error("查询联系人喜欢列表失败~ ",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 查询公告列表
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("announcements")
    @NoAuthorization//优化，无需进行token校验
    public ResponseEntity<PageResult>queryMessageAnnouncementList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                           @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult= this.imService.queryMessageAnnouncementList(page,pageSize);
            if(null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            LOGGER.error("查询公告列表失败~ ",e);
        }
       return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
